// pages/myself/store/StoreInfo.js
var Zan = require('../../../dist/index');
var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');
var network = null;
var utils = null;
var api = null;
const app = getApp();
var start = 0;

Page(Object.assign({}, Zan.TopTips, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    shopInfo: [],
    radioCheckVal: 0,
    address: '点击获取当前位置',
    longitude: '',
    latitude: '',
    orderId: null,
    orderNO: null,
    orderDesc: null,
    againPay: 0,
    nodata: {
      msg: '点击定位获取附近门店',
      notype: 2,
      imgurl: '/images/nodatas.png',
      nourl: ''
    },
    key: "BKUBZ-QJOCS-UY2O7-6Y3MK-42OOE-BABER",
    gototype: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    var gototype = options.gototype;
    this.setData({
      gototype: options.gototype,
      mode2: options.mode,
      pagepath: options.pagepath,
      deliverytime: options.deliverytime
    })
    if (options.orderNO) {
      this.setData({
        orderId: options.orderId,
        orderNO: options.orderNO,
        orderDesc: options.orderDesc,
        againPay: options.againPay
      })
    }
    // var that = this;
    // var qqmapsdk = new QQMapWX({
    //   key: that.data.key
    // });
    // wx.showLoading({
    //   title: '',
    // })
    // // 发起reverseGeocoder检索请求
    // qqmapsdk.reverseGeocoder({
    //   success: function (res) {
    //     wx.hideLoading();
    //     that.setData({
    //       latitude: res.result.ad_info.location.lat,
    //       longitude: res.result.ad_info.location.lng,
    //       address: res.result.address,
    //       addrdesc: res.result.formatted_addresses.recommend
    //     })
    //     network._post_page_json(that, api.storeList, start, {dimension: that.data.latitude, longitude: that.data.longitude}, success);
    //   }
    // }); 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    start = 0;
    wx.stopPullDownRefresh();
    this.setData({
      shopInfo: [],
    });
    network._post_page_json(this, api.storeList, start, { dimension: that.data.latitude, longitude: that.data.longitude }, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.shopInfo.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_page_json(this, api.storeList, start, { dimension: that.data.latitude, longitude: that.data.longitude }, success);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  toDetails: function (e) {
    if (this.data.gototype == 1) {
      wx.setStorageSync("shop_details", e.currentTarget.dataset.item);

      WxNotificationCenter.postNotificationName("RefreshNotification");
      wx.navigateBack();
      // if (this.data.pagepath == 1) {
      //   if (this.data.orderNO) {
      //     wx.navigateTo({
      //       url: "/pages/pay/Pay?mode2=" + this.data.mode2 + "&deliverytime=" + this.data.deliverytime + '&orderId=' + this.data.orderId + '&orderNO=' + this.data.orderNO + '&againPay=' + this.data.againPay + '&orderDesc=' + this.data.orderDesc
      //     })
      //   } else {
      //     wx.navigateTo({
      //       url: "/pages/pay/Pay?mode2=" + this.data.mode2 + "&deliverytime=" + this.data.deliverytime
      //     })
      //   }

      // } else if (this.data.pagepath == 2) {
      //   wx.navigateTo({
      //     url: "/pages/pay/SpellPay?mode2=" + this.data.mode2 + "&deliverytime=" + this.data.deliverytime
      //   })
      // } else if (this.data.pagepath == 3) {
      //   // wx.navigateTo({
      //   //   url: "/pages/pay/IntegralPay?mode2=" + this.data.mode2 + "&deliverytime=" + this.data.deliverytime
      //   // })
      //   wx.navigateBack();
      // }

    } else {
      // let item = JSON.stringify(e.currentTarget.dataset.item);
      // wx.setStorageSync("shop_details", e.currentTarget.dataset.item);
      wx.navigateTo({
        url: '/pages/myself/store/StoreDetails?linkId=' + e.currentTarget.dataset.item.id
      })
    }

  },
  goMap: function (e) {
    //114.30125,30.525687
    //<input type="text" id= "add-text1" value= "114.300917,30.529462" class="form-control input-large" name= "addressGeo" >
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          address: res.address,
          latitude: res.latitude,
          longitude: res.longitude
        })
        that.setData({
          shopInfo: []
        })
        network._post_page_json(that, api.storeList, start, { dimension: that.data.latitude, longitude: that.data.longitude }, success);
      }
    })
    // var site = e.currentTarget.dataset.site;
    // var name = e.currentTarget.dataset.name;
    // var address = this.data.details.province + this.data.details.city + this.data.details.district + this.data.details.address;
    // var addressGeo = site.split(",");
    // wx.openLocation({
    //   type: 'wgs84 ',
    //   latitude: parseFloat(addressGeo[1]),
    //   longitude: parseFloat(addressGeo[0]),
    //   // latitude: 30.525687,
    //   // longitude: 114.30125,
    //   name: name,
    //   address: address,
    //   scale: 18
    // });
  }
}))

// 获取门店列表结果
var success = function (that, res) {
  if (res.data.data && res.data.data.length>0) {
    that.setData({
      shopInfo: that.data.shopInfo.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
  }else{
    that.showZanToast('暂时没有开放门店，请选择物流配送！');
  }
}